package org.bronycorps.mlptss.domain.controller;

import org.bronycorps.mlptss.domain.model.Bonus;
import org.bronycorps.mlptss.manager.preferences.GameConfiguration;
import org.bronycorps.mlptss.screen.LevelScreen;

import com.badlogic.gdx.math.Vector2;

public class BonusController extends AbstractController {

	private float direction;
	private Bonus model;
	private float speed;

	public BonusController(Bonus model) {
		super();
		this.model = model;

		// load config
		this.speed = GameConfiguration.getInstance().getBonusSpeed();

		// always appears at same place
		this.direction = 135;
		model.getPosition().set(LevelScreen.STAGE_WIDTH / 2, 0);
	}

	@Override
	public void act(float delta) {

		this.model.reduceCooldowns(delta);

		// create a simple bouncing ball movement

		// prepare velocity vector
		Vector2 velocity = new Vector2(this.speed * delta, 0);
		velocity.setAngle(this.direction);

		float actualX = this.model.getPosition().x;
		float actualY = this.model.getPosition().y;

		Vector2 newPosition = this.model.getPosition().cpy().add(velocity);
		// don't bounce if bonus is still not in screen
		if (actualX > 50 && actualX < LevelScreen.STAGE_WIDTH - 50
				&& actualY > 20 && actualY < LevelScreen.STAGE_HEIGHT / 2 - 20) {
			// check bounce on X
			if (newPosition.x < 50
					|| newPosition.x > LevelScreen.STAGE_WIDTH - 50) {
				velocity.x = -velocity.x;
				newPosition = this.model.getPosition().cpy().add(velocity);
			}
			// check bounce on Y
			if (newPosition.y < 20
					|| newPosition.y > LevelScreen.STAGE_HEIGHT / 2 - 20) {
				velocity.y = -velocity.y;
				newPosition = this.model.getPosition().cpy().add(velocity);
			}
		}

		this.model.getPosition().set(newPosition);
		this.direction = velocity.angle();

	}

	public Bonus getModel() {
		return this.model;
	}

}
